package com.java2007.hotel.service.impl;

import com.java2007.hotel.dao.DinnerTableDao;
import com.java2007.hotel.dao.impl.DinnerTableDaoImpl;
import com.java2007.hotel.pojo.DinnerTable;
import com.java2007.hotel.service.DinnerTableService;
import com.java2007.hotel.utils.JdbcUtils;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * @author Reckeys
 * @version V1.0
 * @date 2021/01/26
 **/
public class DinnerTableServiceImpl implements DinnerTableService {
    private DinnerTableDao dinnerTableDao = new DinnerTableDaoImpl();

    /**
     * 通过餐桌状态查询对应状态的餐桌
     *
     * @param tableStatus
     * @return
     */
    @Override
    public List<DinnerTable> findDinnerTableByStatus(int tableStatus) throws SQLException {
        if (tableStatus != 0 && tableStatus != 1) {
            return null;
        }
        List<DinnerTable> dinnerTableList = dinnerTableDao.findDinnerTableByStatus(tableStatus);
        return dinnerTableList;
    }

    /**
     * 按名字查询餐桌，如果名字为null则查询所有
     *
     * @param tableName
     * @return
     */
    @Override
    public List<DinnerTable> findDinnerTableByName(String tableName) throws SQLException {
        if (tableName == null) {
            tableName = "";
        }
        List<DinnerTable> dinnerTableList = dinnerTableDao.findDinnerTableByName("%" + tableName.trim() + "%");
        return dinnerTableList;
    }

    /**
     * 通过餐桌id更改预定状态和预定时间
     *
     * @param tableId
     * @param tableScheduledTime
     * @return
     */
    @Override
    public String updateDinnerTableStatus(Integer tableId, Date tableScheduledTime) throws SQLException {
        //查询对应id的餐桌信息
        DinnerTable dinnerTable = dinnerTableDao.findDinnerTableByDinnerTableId(tableId);
        Integer tableStatus = dinnerTable.getTableStatus();
        try {
            JdbcUtils.startTransactions();
            //状态为空闲时，预约
            if (tableStatus == 0) {
                if (tableScheduledTime == null){
                    return "时间格式错误";
                }
                dinnerTable.setTableScheduledTime(tableScheduledTime);
                dinnerTable.setTableStatus(1);
                dinnerTableDao.updateDinnerTableStatus(dinnerTable);
                JdbcUtils.commit();
                return "预约成功";
            }

            //通过状态为预约，将餐桌设置为空闲
            if (tableStatus == 1){
                dinnerTable.setTableScheduledTime(null);
                dinnerTable.setTableStatus(0);
                dinnerTableDao.updateDinnerTableStatus(dinnerTable);
                JdbcUtils.commit();
                return "退桌成功";
            }
        } catch (Exception e) {
            e.printStackTrace();
            JdbcUtils.rollBack();
        }
        return "操作失败";

    }

    /**
     * 通过tableId删除餐桌
     * @param tableId
     * @return
     */
    @Override
    public int deleteDinnerTableByTableId(Integer tableId) {
        try {
            JdbcUtils.startTransactions();
            dinnerTableDao.deleteDinnerTableByTableId(tableId);
            JdbcUtils.commit();
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            JdbcUtils.rollBack();
        }
        return 0;
    }

    /**
     * 添加餐桌
     * @param dinnerTable
     * @return
     */
    @Override
    public int addDinnerTable(DinnerTable dinnerTable) {
        dinnerTable.setTableCreateTime(new Date());
        dinnerTable.setTableStatus(0);
        dinnerTable.setTableUpdateTime(new Date());
        try {
            JdbcUtils.startTransactions();
            dinnerTableDao.addDinnerTable(dinnerTable);
            JdbcUtils.commit();
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            JdbcUtils.rollBack();
            return 0;
        }
    }

    /**
     * 通过餐桌id查询餐桌
     * @param tableId
     */
    @Override
    public DinnerTable findDinnerTableByTableId(Integer tableId) throws SQLException {
        return dinnerTableDao.findDinnerTableByTableId(tableId);
    }


}
